package org.ielts.repository;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.ielts.domain.base.Teaching;
import org.ielts.domain.report.TeachingWithCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.yifeng.dao.HibernateRepository;

@Repository
public class TeachingRepository extends HibernateRepository<Teaching, String> {

	public Iterable<Teaching> findBetweenDate(Date startDate, Date endDate) {
		DetachedCriteria detachedCriteria = createDetachedCriteria();

		detachedCriteria.add(Restrictions.between("beginTime", startDate, endDate)).setResultTransformer(
				Criteria.DISTINCT_ROOT_ENTITY);

		@SuppressWarnings("unchecked")
		List<Teaching> teachings = hibernateTemplate.findByCriteria(detachedCriteria);

		return teachings;
	}

	public Iterable<Teaching> findBetweenDateAndTeacher(Date startDate, Date endDate, String teacherId) {
		DetachedCriteria detachedCriteria = createDetachedCriteria();

		detachedCriteria.add(Restrictions.between("beginTime", startDate, endDate)).createCriteria("teacher")
				.add(Restrictions.eq("id", teacherId));

		@SuppressWarnings("unchecked")
		List<Teaching> teachings = hibernateTemplate.findByCriteria(detachedCriteria);

		return teachings;
	}

	public Iterable<TeachingWithCount> findReportBetweenDateAndTeacher(Date beginTime, Date endTime, String teacherId) {
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();

		String hql = "select new org.ielts.domain.report.TeachingWithCount(t, "
				+ "(select count(o.id) from OrderLessonLog o where o.lesson.id = t.lesson.id) as numOfListeners )"
				+ "from Teaching t "
				+ "where t.teacher.id = :teacherId and t.beginTime between :beginTime and :endTime order by t.beginTime";

		Query query = session.createQuery(hql);

		query.setString("teacherId", teacherId);
		query.setTimestamp("beginTime", beginTime);
		query.setTimestamp("endTime", endTime);

		@SuppressWarnings("unchecked")
		Iterable<TeachingWithCount> list = query.list();

		return list;

	}

	public Iterable<TeachingWithCount> findReportBetweenDateAndTeacherAndCampus(Date beginTime, Date endTime,
			String teacherId, String campusId) {
		Session session = hibernateTemplate.getSessionFactory().getCurrentSession();

		String hql = "select new org.ielts.domain.report.TeachingWithCount(t, "
				+ "(select count(o.id) from OrderLessonLog o where o.lesson.id = t.lesson.id) as numOfListeners )"
				+ "from Teaching t "
				+ "where t.teacher.id = :teacherId and t.beginTime between :beginTime and :endTime and t.lesson.classroom.campus.id = :campus order by t.beginTime";

		Query query = session.createQuery(hql);

		query.setString("teacherId", teacherId);
		query.setTimestamp("beginTime", beginTime);
		query.setTimestamp("endTime", endTime);
		query.setString("campus", campusId);

		@SuppressWarnings("unchecked")
		Iterable<TeachingWithCount> list = query.list();

		return list;
	}

	public Iterable<Teaching> findBetweenDate(Date startDate, Date endDate, String campusId) {
		DetachedCriteria criteria = createDetachedCriteria();

		criteria.add(Restrictions.between("beginTime", startDate, endDate)).createCriteria("lesson")
				.createCriteria("classroom").createCriteria("campus").add(Restrictions.eq("id", campusId))
				.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

		@SuppressWarnings("unchecked")
		List<Teaching> teachings = hibernateTemplate.findByCriteria(criteria);

		return teachings;

	}
}
